<include file="../header" />
</head>
<body class="J_scroll_fixed">
<script type="text/javascript">

    function onlyNumber(obj){
        //得到第一个字符是否为负号
        var t = obj.value.charAt(0);

        //先把非数字的都替换掉，除了数字和.
        if(obj.value != 0)
        obj.value = obj.value.replace(/[^\d\.]/g,'').replace(/^0{1,}/g,'');
        //必须保证第一个为数字而不是.
        obj.value = obj.value.replace(/^\./g,'');
        //保证只有出现一个.而没有多个.
        obj.value = obj.value.replace(/\.{2,}/g,'.');
        //保证.只出现一次，而不能出现两次以上
        obj.value = obj.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
        //只能输入两位小数
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
        //如果第一位是负号，则允许添加
        if(t == '-'){
            return;
        }

        var index = obj.name.substring(obj.name.indexOf("_") + 1, obj.name.length);
        var numberName = "number_" + index;
        if(obj.value == 0){
            $("input[name="+numberName+"]").css('border-color',"#dce4ec");
        }else{
            $("input[name="+numberName+"]").css('border-color',"#ed7667");
        }
        var total = parseInt($("input[name=hospital_order_number]").val());
        total += (parseInt(obj.value) - parseInt($("input[name=number_value_"+index+"]").val()));
        $("input[name=number_value_"+index+"]").val(obj.value);
        $("input[name=hospital_order_number]").val(total);
        if(obj.value != $("input[name=number_value_ori_"+index+"]").val()){
            $("input[name="+numberName+"]").css('background-color',"#fbdfe4");
        }else{
            $("input[name="+numberName+"]").css('background-color',"#ffffff");
        }
    }

    function checkCreateDate(obj){
        if(obj.value != $("input[name=create_date_ori]").val()){
            $("input[name=create_date]").css('background-color',"#fbdfe4");
        }else{
            $("input[name=create_date]").css('background-color',"#ffffff");
        }
    }
    function checkOpDate(obj){
        if(obj.value != $("input[name=op_date_ori]").val()){
            $("input[name=op_date]").css('background-color',"#fbdfe4");
        }else{
            $("input[name=op_date]").css('background-color',"#ffffff");
        }
    }
    function checkDoctor(obj){
        if(obj.value != $("input[name=doctor_name_ori]").val()){
            $("input[name=doctor_name]").css('background-color',"#fbdfe4");
        }else{
            $("input[name=doctor_name]").css('background-color',"#ffffff");
        }
    }
    function checkPatient(obj){
        if(obj.value != $("input[name=patient_name_ori]").val()){
            $("input[name=patient_name]").css('background-color',"#fbdfe4");
        }else{
            $("input[name=patient_name]").css('background-color',"#ffffff");
        }
    }
    function checkNote(obj){
        if(obj.value != $("input[name=note_ori]").val()){
            $("input[name=note]").css('background-color',"#fbdfe4");
        }else{
            $("input[name=note]").css('background-color',"#ffffff");
        }
    }

    $(function() {

        $("input[name=completed]").click(function() {
            if($("input[name=completed]").is(':checked')) {
                $("button.btn_submit").css("background-color","#52b1ef");
                $("button.btn_submit").text("出库");
                $("input[name=closed]").attr("checked", false);
                $("input[name=close_reason]").hide();
            }else{
                $("button.btn_submit").css("background-color","#e99a3e");
                $("button.btn_submit").text("暂存");
            }
        });

        $("input[name=closed]").click(function() {
            if($("input[name=closed]").is(':checked')) {
                $("button.btn_submit").css("background-color","#ed7667");
                $("button.btn_submit").text("关闭");
                $("input[name=completed]").attr("checked", false);
                $("input[name=close_reason]").show();
            }else{
                $("button.btn_submit").css("background-color","#e99a3e");
                $("button.btn_submit").text("暂存");
                $("input[name=close_reason]").hide();
            }
        });

        $("button.btn_submit").click(function() {

            var diffText = "";
//            if(reportList != "{$hospitalOrder.report_list}") {
//                diffText += "* 订单列表\r\n";
//            }
            if($("input[name=create_date]").val() != "{$hospitalOrder.create_date}") {
                diffText += "* 下单日期\r\n";
            }
            if($("input[name=op_date]").val() != "{$hospitalOrder.op_date}") {
                diffText += "* 手术日期\r\n";
            }
            if($("input[name=doctor_name]").val() != "{$hospitalOrder.doctor_name}") {
                diffText += "* 手术医生\r\n";
            }
            if($("input[name=patient_name]").val() != "{$hospitalOrder.patient_name}") {
                diffText += "* 病人姓名\r\n";
            }
            if($("input[name=note]").val() != "{$hospitalOrder.note}") {
                diffText += "* 备注\r\n";
            }

            var rowNum= $("#viewTabs tr").size() - 1;
            for (var i = 0;  i < rowNum; i++) {
                if($("input[name=number_"+i+"]").css('background-color').toString() != "rgb(255, 255, 255)"){
                    diffText += "* 订单列表\r\n";
                    break;
                }
            }
            var confirmText = "";
            if($("input[name=completed]").is(':checked')) {
                if($("input[name=hospital_order_number]").val() == 0){
                    alert("【订单产品】为空");
                    return false;
                }

                if ($("input[name=create_date]").val() == 0) {
                    alert("请选择【下单时间】");
                    $("input[name=create_date]").focus();
                    return false;
                }

                if ($("input[name=op_date]").val() == 0) {
                    alert("请选择【手术时间】");
                    $("input[name=op_date]").focus();
                    return false;
                }
                if ($("input[name=doctor_name]").val() == 0) {
                    alert("请填写【手术医生】");
                    $("input[name=doctor_name]").focus();
                    return false;
                }
                if ($("input[name=patient_name]").val() == 0) {
                    alert("请填写【病人姓名】");
                    $("input[name=patient_name]").focus();
                    return false;
                }

                if(diffText != ""){
                    confirmText = "您修改了以下订单内容，请确认是否出库？\r\n\n" + diffText;
                }else{
                    confirmText = "请确认是否出库？"
                }
            }else if($("input[name=closed]").is(':checked')) {
                if ($("input[name=close_reason]").val() == 0) {
                    alert("请填写关闭原因");
                    $("input[name=close_reason]").focus();
                    return false;
                }
                confirmText = "请确认是否关闭此订单？"
            }
            else{
                if(diffText != ""){
                    confirmText = "您修改了以下订单内容，请确认是否暂存？\r\n\n" + diffText;
                }else{
                    alert("内容未做改动，无需暂存");
                    return false;
                }
            }

            if (confirm(confirmText)) {
                $("button.btn_submit").hide();
            } else {
                return false;
            }

        });

    });

</script>
<style type="text/css">
    .btn,.btn-danger,.btn-info,.btn-success,.btn-warning{
        width: 50px;
        /*height: 18px;*/
        padding: 2px 0px 2px 0px;
        margin: 1px 0px 1px 0px;
    }
    .number{
        width: 40px;
    }
    .J_date{
        width: 100px;
    }
    th{
        line-height: 5px
    }
</style>
<div class="wrap jj">
    <div class="common-form">
        <form method="post" class="form-horizontal J_ajaxForm" action="{:U('HospitalOrder/outStockPost')}">
            <a style="color:#3fcca9;font-weight: bold;font-size: large">{$hospital.hospital_name}</a>
            <if condition="$hospitalOrder['subhospital_name'] neq ''">
                <a style="font-weight: bold;font-size: large ">( {$hospitalOrder.subhospital_name} )</a>
            </if>
            <a style="color:#7171cc;font-weight: bold;font-size: large">【{$hospitalOperation.hospital_operation_name}】</a>
            &nbsp;&nbsp;&nbsp;
            共计 <input name="hospital_order_number" style="color:#e97a15;font-weight: bold;font-size: large;width: 35px;text-align: center" type="text" value="{$hospitalOrder.number}" readonly/> 件
            &nbsp;&nbsp;&nbsp;
            <if condition="$hospitalOrder['status'] eq '1'">
                <a style="color: #45a1de;font-weight: bold;font-size: large">（已出库）</a>
            </if>
            <if condition="$hospitalOrder['status'] eq '-1'">
                <a style="color: #ed7667;font-weight: bold;font-size: large">（未出库）</a>
            </if>
            <if condition="$hospitalOrder['status'] eq '-2'">
                <a style="color: #ed7667;font-weight: bold;font-size: large">（未提交）</a>
            </if>
            <br/><br/>
            <input type="hidden" name="id" value="{$hospitalOrder.hospital_order_id}" />
            <fieldset>
                <div class="control-group">
                    单号：<input type="text" name="hospital_order_no" value="{$hospitalOrder.hospital_order_no}" readonly/>
                    &nbsp;&nbsp;&nbsp;
                    下单人：
                    <input type="text" style="width: 100px" value="{$updater.admin_name}" readonly/>
                    &nbsp;&nbsp;&nbsp;
                    更新时间：
                    <input type="text" value="{$hospitalOrder.update_time}" readonly/>
                    <br><br>
                    下单日期：
                    <input type="text" class="input J_date" style="width: 100px" name="create_date" value="{$hospitalOrder.create_date}" onblur="checkCreateDate(this)">
                    <input type="hidden" name="create_date_ori" value="{$hospitalOrder.create_date}"/>
                    &nbsp;&nbsp;&nbsp;
                    手术日期：
                    <input type="text" class="input J_date" style="width: 100px" name="op_date" value="{$hospitalOrder.op_date}" onblur="checkOpDate(this)">
                    <input type="hidden" name="op_date_ori" value="{$hospitalOrder.op_date}"/>
                    &nbsp;&nbsp;&nbsp;
                    手术医生：
                    <input type="text" style="width: 100px" name="doctor_name" value="{$hospitalOrder.doctor_name}" onkeyup="checkDoctor(this)"/>
                    <input type="hidden" name="doctor_name_ori" value="{$hospitalOrder.doctor_name}"/>
                    &nbsp;&nbsp;&nbsp;&nbsp;
                    病人姓名：
                    <input type="text" style="width: 200px" name="patient_name" value="{$hospitalOrder.patient_name}" onkeyup="checkPatient(this)"/>
                    <input type="hidden" name="patient_name_ori" value="{$hospitalOrder.patient_name}"/>
                    <!--供应商：
                    <select name="supplier_id">
                        {:getCommonSelectOptionHtml($hospitalOrder['supplier_id'], "Supplier", "supplier_id", "supplier_name", null, $user['enterprise_code'], 1)}
                    </select>-->
                    <br/><br/>
                    备注：
                    <input type="text" class="input" style="width: 790px" name="note" value="{$hospitalOrder.note}" onkeyup="checkNote(this)">
                    <input type="hidden" name="note_ori" value="{$hospitalOrder.note}"/>
                </div>
                <hr class="half-rule">

                <table class="table table-hover table-bordered" id="viewTabs">
                    <thead>
                    <tr>
                        <th style="min-width: 30px; width: 30px">序号</th>
                        <th style="width: 110px">产品编号</th>
                        <th style="width: 50px">数量</th>
                        <th style="width: auto">产品描述</th>

                    </tr>
                    </thead>
                    <tbody>
                    <foreach name="list" item="vo" key="k">
                        <tr>
                            <td style="text-align: center;line-height: 5px;vertical-align: middle">
                                {$k+1}
                                <input type="hidden" class="input" name="hospital_product_id_{$k}" value="{$vo.hospital_product_id}">
                                <input type="hidden" class="input" name="hospital_product_name_{$k}" value="{$vo.hospital_product_name}">
                                <input type="hidden" class="input" name="number_value_{$k}" value="{$vo.number}">
                                <input type="hidden" class="input" name="number_value_ori_{$k}" value="{$vo.number}">
                            </td>
                            <td style="line-height: 5px;vertical-align: middle">
                                <a style="color: #7171cc;font-weight: bold">{$vo.code}</a>
                            </td>
                            <if condition="$vo['number'] eq '0'">
                                <td style="line-height: 5px;vertical-align: middle"><input style="padding: 3px 0px 3px 0px;text-align: center" type="text" class="input number" name="number_{$k}" value="{$vo.number}" onkeyup="onlyNumber(this)"></td>
                                <else/>
                                <td style="line-height: 5px;vertical-align: middle"><input style="border-color: #ed7667;font-weight: bold; padding: 3px 0px 3px 0px; text-align: center" type="text" class="input number" name="number_{$k}" value="{$vo.number}" onkeyup="onlyNumber(this)"></td>
                            </if>
                            <td style="line-height: 5px;vertical-align: middle">
                                <a style="color: #45a1de;">{$vo.hospital_product_name}</a>
                            </td>

                        </tr>
                    </foreach>
                    </tbody>
                </table>

                <input type="hidden" name="row" value="{$row}"/>

                <div class="control-group">
                    <div class="controls">
                        <label class="checkbox inline" style="font-weight: bold">
                            <input type="checkbox" name="completed"> <a style="color: #52b1ef">确认出库（勾选后出库将无法再次修改订单）</a>
                        </label>
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <label class="checkbox inline" style="font-weight: bold">
                            <if condition="$closed eq 'yes'">
                                <input type="checkbox" name="closed" checked> <a style="color: #ed7667; font-weight: bold">关闭订单</a>
                                <input type="text" class="input" style="width: 390px; color: #ed7667;" name="close_reason" value="{$hospitalOrder.close_reason}" placeholder="请填写关闭原因">
                            <else/>
                                <input type="checkbox" name="closed"> <a style="color: #ed7667; font-weight: bold">关闭订单</a>
                                <input type="text" class="input" style="width: 390px; color: #ed7667; display: none" name="close_reason" value="{$hospitalOrder.close_reason}" placeholder="请填写关闭原因">
                            </if>
                        </label>
                    </div>
                </div>

            </fieldset>
            <div class="form-actions">
                <php>
                    $btnText = "暂存";
                    $btnStatus = -1;
                    $role = explode(',',$user["role"]);
                    if(in_role($role,"1,13")){
                        if($hospitalOrder["status"] == -1 || isNullOrEmpty($hospitalOrder["status"])){
                            if($closed == "yes"){
                                $btnText = "关闭";
                                $btnStatus = -2;
                            }
                            else{
                                $btnText = "暂存";
                                $btnStatus = 1;
                            }

                        }else{
                            $btnText = "已出库";
                            $btnStatus = -1;
                        }
                    }
                </php>
                <if condition="$btnStatus eq 1">
                    <button type="submit" style="width: 70px; background-color: #e99a3e" class="btn btn-primary btn_submit ">{$btnText}</button>
                </if>
                <if condition="$btnStatus eq -2">
                    <button type="submit" style="width: 70px; background-color: #ed7667" class="btn btn-primary btn_submit ">{$btnText}</button>
                </if>
                <if condition="$btnStatus eq -1">
                    <button type="submit" style="width: 70px;" disabled class="btn btn-primary btn_submit ">{$btnText}</button>
                </if>
                <input type="hidden" name="btnText" value="{$btnText}"/>
                <a style="width: 70px" class="btn" href="__URL__">返回</a>
            </div>
        </form>
    </div>
</div>
<script src="__ROOT__/statics/js/common.js"></script>
</body>
</html>